// 创建商品一二三分类的小仓库
import { defineStore } from 'pinia'
import { reqC1, reqC2, reqC3 } from '@/api/product/attr' //引入分类接口
import type { CategoryResponseData } from '@/api/product/attr/type'
import type { CategoryState } from './types/type'

const useCategoryStore = defineStore('Category', {
  state: (): CategoryState => {
    return {
      c1Arr: [], // 存放一级分类内容
      c1Id: '', //存放一级选中项的id
      c2Arr: [], // 存放二级分类内容
      c2Id: '', //存放二级选中项的id
      c3Arr: [], // 存放三级分类内容
      c3Id: '', //存放三级选中项的id
    }
  },
  actions: {
    // 获取一级分类接口内容
    async getC1Store() {
      const res: CategoryResponseData = await reqC1()
      // 如果状态码为200 就是成功
      if (res.code == 200) {
        this.c1Arr = res.data
      }
    },
    // 获取一级分类下二级分类的接口内容
    async getC2Store() {
      const res: CategoryResponseData = await reqC2(this.c1Id)
      if (res.code == 200) {
        this.c2Arr = res.data
      }
    },
    // 获取二级分类下三级分类的接口内容
    async getC3Store() {
      const res: CategoryResponseData = await reqC3(this.c2Id)
      if (res.code == 200) {
        this.c3Arr = res.data
      }
    },
  },
  getters: {},
})
export default useCategoryStore
